//Core
(function(){

	var YD = YAHOO.util.Dom,YE = YAHOO.util.Event;

 	YAHOO.namespace('YAHOO.Zergling');

	var zergling = function(){

		//模块信息
		var ModuleInfo = function(){
			this.init.apply(this,arguments);
		};
		ModuleInfo.prototype = {
			init:function(_name,_cfg){
				this.loader =  new YAHOO.util.YUILoader();
				this.name = _name;
				this.creater = _cfg.creater;
				this.require = _cfg.require;
				try{
					this.handler = _cfg.handler();
				}catch(_e){
					alert('Zergling Error\n'+_e.message+'\n'+this);
				//	var err = new Error(); 
				//	err.name = 'Zergling Error'; 
				//	err.message = msg;
				//	throw err;
				}
				this.handler.init.call(this,null);
				YE.onDOMReady(this.loadWidgets,this,this);
			},
			loadWidgets:function(){
				this.loader.require(this.require);
				var __module = this;
				this.loader.onSuccess = function(){
					__module.handler.onPageReady.call(__module,null);
				};
				this.loader.insert();
			},
			toString:function(){
				var str = ['module:'+this.name,'creater:['+YAHOO.Zergling.Developer.get(this.creater).toString()+']'];
				return str.join('.');
			},
			fire:function(_type,_arg){
				zeventManager.fire(_type,this);
			},
			unsubscribe:function(){
			}
		};

		//模块管理
		var moduleManager = function(){
			var modules = {};
			return {
				items:modules,
				send:function(_type,_arg){
					for(var m_name in modules){
						if(m_name){
							if(!!modules[m_name].handler[_type]){
								modules[m_name].handler[_type].call(modules[m_name],_arg);
							}
						}
					}
				},
				add:function(_name,_module){
					modules[_name] = new ModuleInfo(_name,_module);
				}
			};
		};
		moduleManager = moduleManager();

		//开发者信息
		var DeveloperInfo = function(){
			this.init.apply(this,arguments);
		};
		DeveloperInfo.prototype = {
			init:function(_name,_cfg){
				this.name = _name;
				this.phone = _cfg.phone;
				this.email = _cfg.email;
				this.im = _cfg.im;
			},
			toString:function(){
				var str = ['name:'+this.name,'phone:'+this.phone];
				return str;
			}
		};

		//开发者管理
		var developerManager = function(){
			var developers = {};
			return {
				items:developers,
				get:function(_name){
					if(developers[_name]){
						return developers[_name];
					}else{
						return new DeveloperInfo('unknow',{phone:'unknow'});
					}
				},
				register:function(_name,_cfg){
					developers[_name] = new DeveloperInfo(_name,_cfg);
				}
			};
		};
		developerManager = developerManager();

		/*
		//事件信息类
		var Zevent = function(){
			this.init.apply(this,arguments);
		};
		Zevent.prototype = {
			init:function(_ce,_handler){
				this.name = _ce;
				this.handler = _handler;
			}
		};
		*/

		//event
		var zeventManager = function(){
			var events = {};
			return {
				efire:function(_ce,_arg){
					if(events[_ce]){
						for(var i=0; i<events[_ce].length;i++){
							if(events[_ce][i]){
								if(arguments.length>1){
									events[_ce][i](_arg);
								}else{
									events[_ce][i]();
								}
							}
						}
					}
				},
				fire:function(_ce,_arg){
					moduleManager.send(_ce,_arg);
					this.efire(_ce,_arg);
				},
				subscribe:function(_ce,_fun){
					if(!events[_ce]){
						events[_ce] = [];
					}
					events[_ce].push(_fun);
			  }
			};
		};
		zeventManager = zeventManager();


		//fire event onPageRender
		YE.on(window,'load',
			function(){
				zeventManager.fire('onPageLoaded');
			}
		);

		return {
			ModuleInfo:ModuleInfo,
			Module:moduleManager,
			DeveloperInfo:DeveloperInfo,
			Developer:developerManager,
			Event:zeventManager
		};
	};

	YAHOO.Zergling = zergling();

})();



